<?php
$loader->requireOnce('includes/Icd10Selector/Icd10Selector.class.php');
 
class C_GeneralExamination extends Controller {
		
	const ENABLE_SECONDARY_ICD = false;
	private $icd10Selector;
	
	public function C_GeneralExamination($template_mod = 'general') {
		parent::Controller();
		$this->template_mod = $template_mod;
		$this->view->path = 'general_examination';
		
		$this->icd10Selector = new Icd10Selector();
		$this->logger = LoggerManager::getLogger('modules.general_emr.C_GeneralExamination');
	}
	
	public function actionLoadGeneralExamination() {
		$this->logger->info('Start actionLoadGeneralExamination');
		
		$recordId	=	$_GET['recordId'];
		$encounter	=	ORDataObject::factory('EmrGeneralExamination')->getObjectFromRecordId($recordId);
		if($encounter->get_id() > 0){
			$doctorName	=	$encounter->getDoctorName();
			//$doctorUpdate = $encounter->getUpdatedDoctorName();
			$doctorId	=	$encounter->getDoctorId();
			//$created = L10nHelper::renderDateTime($encounter->get('encounter_date'), 'short', 'datetime');
			$updated = L10nHelper::renderDateTime($encounter->get('last_updated'), 'short', 'datetime');
			
		} else {
			$person		= ORDataObject::factory("Employee");
			$person->setup($this->_me->get_person_id());
			$doctorName	=	$person->getName();
			$doctorId	=	$person->get_id();
			//$doctorUpdate = $doctorName;
			//$created = L10nHelper::renderDateTime(date('Y-m-d H:m:s'), 'short', 'datetime');
			$updated = L10nHelper::renderDateTime(date('Y-m-d H:m:s'), 'short', 'datetime');
		}

		$icd10		=	ORDataObject::factory('Icd10')->getObjectbyCode($encounter->getPrimaryIcd10());		
		
        $this->icd10Selector->initIcd10ForController($this, 6);
        
		$patientId = Celini::newORDO('MedicalRecord', $recordId)->get('patient_id');

		$this->assign_by_ref('patient', Celini::newORDO('Patient', $patientId));
		$this->assign_by_ref('priIcd10', $icd10);
		$this->assign_by_ref('encounter', $encounter);
		
		$this->assign('doctorName', $doctorName);
		$this->assign('doctorId', $doctorId);
		//$this->assign('doctorUpdate', $doctorUpdate);
		//$this->assign('createdDate', $created);
		$this->assign('updatedDate', $updated);
		$this->assign('recordId', $recordId);

		if (self::ENABLE_SECONDARY_ICD) {
			$icd10_1	=	ORDataObject::factory('Icd10');
			$icd10_1->setup($encounter->getSecondaryIcd10_1());
			$icd10_2	=	ORDataObject::factory('Icd10');
			$icd10_2->setup($encounter->getSecondaryIcd10_2());
			$this->assign_by_ref('sec1Icd10', $icd10_1);
			$this->assign_by_ref('sec2Icd10', $icd10_2);
		}
		$this->assign('enableSecondary', self::ENABLE_SECONDARY_ICD);
		
		$this->logger->info('Finish actionLoadGeneralExamination');

		return $this->view->render('edit_general_examination.html');
		
	}
	
	public function actionEditGeneralExamination() {
	    $emr = $_POST['emr'];
	    
		$primIcd10Code	=	Utils::sanitizeText($emr['primary_icd10']);
		
		if ($primIcd10Code != '' && $this->icd10Selector->validateIcd10Code($primIcd10Code) === false){
			$response['saved']			=	0;
			$response['errorField']		=	"clinDiagPrimaryIcd10";
			$response['errorMessage']	=	L10nHelper::_("Not exist");
			return json_encode($response);
		}
		
		$emr['symptom'] 	= 	Utils::sanitizeText($emr['symptom'], false);
		$emr['diagnosis'] 	= 	Utils::sanitizeText($emr['diagnosis'], false);
		$emr['note'] 		= 	Utils::sanitizeText($emr['note'], false);
		$emr['medical_history']      =   Utils::sanitizeText($emr['medical_history'], false);
		$emr['allergy']      =   Utils::sanitizeText($emr['allergy'], false);
		
		$enId	=(int)	$_POST['encounterId'];
		$encounter	=	Celini::newORDO('EmrGeneralExamination', $enId);
		
		if($_POST['doctorId']!='') {
			$doctorId	=	$emr['doctor_id'];
		} else {
			$doctorId	=	$this->_me->get_person_id();
		}
		//$doctorUpdateId = $this->_me->get_person_id();
		$icd10Id	=	ORDataObject::factory('Icd10')->getObjectbyCode($primIcd10Code)->get_id();

		if (self::ENABLE_SECONDARY_ICD) {
			$secIcd10Code_1	=	Utils::sanitizeText($emr['secondary_icd10_1']);
			
			if($secIcd10Code_1 != '' && !$this->icd10Selector->validateIcd10Code($secIcd10Code_1)){
				$response['saved']			=	0;
				$response['errorField']		=	"clinDiagSecondaryIcd10_1";
				$response['errorMessage']	=	L10nHelper::_("Not exist");
				return json_encode($response);
			}
			$secIcd10Code_2	=	Utils::sanitizeText($emr['secondary_icd10_1']);
			if($secIcd10Code_2 != '' && !$this->icd10Selector->validateIcd10Code($secIcd10Code_2)){
				$response['saved']			=	0;
				$response['errorField']		=	"clinDiagSecondaryIcd10_2";
				$response['errorMessage']	=	L10nHelper::_("Not exist");
				return json_encode($response);
			}
			$icd10Id_1	=	ORDataObject::factory('Icd10')->getObjectbyCode($secIcd10Code_1)->get_id();
			$icd10Id_2	=	ORDataObject::factory('Icd10')->getObjectbyCode($secIcd10Code_2)->get_id();
		} else {
			$icd10Id_1 = '';
			$icd10Id_2 = '';
		}

		$emr['medical_record_id'] = $_POST['recordId'];
		$encounter->populateArray($emr);
		$encounter->persist();
			
		$response['encounterId']=$encounter->get_id();
		$response['saved']            =   1;
		return json_encode($response);
	}
}
?>